package com.hava.mybatis.user.service;

import com.hava.mybatis.user.entity.User;
import com.hava.mybatis.user.repository.GetUserInfoAnnotation;
import com.hava.mybatis.user.repository.UserOp;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * Created by zhanpeng on 09/10/2016.
 */
public class HelloMyBatisMoreOp {

    public static void main(String[] args) {
        moreOp();
    }
    public static void moreOp() {
        // 1. 声明配置⽂件
        String resource = "mybatis-conf.xml";
        // 2. 加载应⽤配置⽂件
        InputStream is = HelloMyBatisMoreOp.class.getClassLoader()
                .getResourceAsStream(resource);
        // 3. 创建SqlSessonFactory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(is);

        SqlSession session = sessionFactory.openSession();
        try {
            // 5. 获取操作类
            UserOp userOp = session.getMapper(UserOp.class);
            User user = new User("XiaoMing", "Netease");
            // 插⼊⽤户
            userOp.addUser(user);
            System.out.println(user.getId());
            // 查询⽤户
            user = userOp.getUser(user.getId());
            System.out.println("userId:" + user.getId() + ", userName:"
                    + user.getUserName() + ", corp:" +
                    user.getCorp());
            user.setUserName("LiMing");
            // 更新⽤户
            userOp.updateUser(user);
            // 删除⽤户
//            userOp.deleteUser(user.getId());

            session.commit();
        } finally {
            // 7.关闭Session
            session.close();
        }
    }
}
